﻿package UIComponents
{
	import MyComponent.MyButton;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	
	public class UIStore extends Sprite
	{
		public var _button_common:MyButton;
		public var _button_weapon:MyButton;
//		public var _button_collect:MyButton;
//		public var _button_medicinal:MyButton;
		public var _obSelectBtn:MyButton;
		public var _jbSelectBtn:MyButton;
		
		public var _kthzBtn:DisplayObject;
		public var _ktnhzBtn:DisplayObject;
		public var _xfhzBtn:DisplayObject;
		public var _xfnhzBtn:DisplayObject;
		
		private var _nowPage:int=1;/*当前页数*/
		private var _maxPage:int;/*最大页数*/
		private var jbArr:Array=new Array();/*金币据源*/
		private var obArr:Array=new Array();/*o币据源*/
		private var currentData:Array;
		/**
		 *   物品 --类型为：101,102,103,104
		 */		
		public  var  package_item_type:Array=[101,102,103,104];
		
		/**
		 *   武器 --类型为：110,111,112,113,114
		 */		
		public var package_weapon_type:Array=[110,111,112,113,114];
		
		/**
		 * 辨别选中的是哪种货币按钮 
		 * O币为：ob；金币为：jb
		 */		
		private var selectFlag:String="ob";
		
		/**
		 * 标志选中的是那种商店物品类型的按钮 ：物品，武器，药品等
		 * 物品为：item ；武器为：weapon ；
		 */		
		private var selectTypeFlag:String="item";
			
		
		
		public function UIStore()
		{
			this.init();
		}
		
		private function init():void
		{
			this._button_common=this.getChildByName("shopItemBtn") as MyButton;
			this._button_weapon=this.getChildByName("shopWeaponBtn") as MyButton;
//			this._button_collect=this.getChildByName("shopCollectionBtn") as MyButton;
//			this._button_medicinal=this.getChildByName("shopMedianBtn") as MyButton;
			this._obSelectBtn=this.getChildByName("ObSelectBtn") as MyButton;
			this._jbSelectBtn=this.getChildByName("JbSelectBtn") as MyButton;
			this._obSelectBtn["lockState"]=true;
			this._kthzBtn=this.getChildByName("_kthzBtn");
			this._ktnhzBtn=this.getChildByName("_ktnhzBtn");
			this._xfhzBtn=this.getChildByName("_xfhzBtn");
			this._xfnhzBtn=this.getChildByName("_xfnhzBtn");
			this.addListener();
			
			//Q币显示
			this.getChildByName("moneySp").visible=FlatInfo.isOpenSeeQb;
			this.getChildByName("ObTxt").visible=FlatInfo.isOpenSeeQb;
			this.getChildByName("button_addOB").visible=FlatInfo.isOpenSeeQb;
			this.getChildByName("qqmoneybg").visible=FlatInfo.isOpenSeeQb;
		}
		
		private function addListener():void
		{
			(this.getChildByName("button_close") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,closeHandler);
			(this.getChildByName("button_addJB") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,addJBHandler);
			this._button_common.addEventListener(ComponentEvent.BUTTON_DOWN,showCommonItemsHandler);
			this._button_weapon.addEventListener(ComponentEvent.BUTTON_DOWN,showWeaponItemsHandler);
			this._jbSelectBtn.addEventListener(ComponentEvent.BUTTON_DOWN,selectJB);
			this._obSelectBtn.addEventListener(ComponentEvent.BUTTON_DOWN,selectOB);
			(this.getChildByName("leftBtn") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnLeftHandler);
			(this.getChildByName("rightBtn") as MyButton).addEventListener(ComponentEvent.BUTTON_DOWN,turnRightHandler);
			this._kthzBtn.addEventListener(MouseEvent.CLICK,kthz);
			this._ktnhzBtn.addEventListener(MouseEvent.CLICK,ktnhz);
			this._xfhzBtn.addEventListener(MouseEvent.CLICK,kthz);
			this._xfnhzBtn.addEventListener(MouseEvent.CLICK,ktnhz);
		}
		
		private function kthz(e:MouseEvent):void
		{
			navigateToURL(new URLRequest(FlatInfo.vipLink));
		}
		private function ktnhz(e:MouseEvent):void
		{
			navigateToURL(new URLRequest(FlatInfo.qqYearVipLink));
		}
		
		public function controlMoenyIcon(qb:Boolean=true):void
		{
			var ms:DisplayObjectContainer=this.getChildByName("moneySp") as DisplayObjectContainer;
			ms.getChildByName("qdian").visible=qb;
			ms.getChildByName("obIcon").visible=!qb;
//			this._kthzBtn.visible=FlatInfo.isOpenHz;
			this.controlHzVisable();
			this.getChildByName("qbwzts").visible=FlatInfo.isOpenHz;
			if(qb)
			{
				this._obSelectBtn.skinsType="ann_qdiandaoju";
			}else{
				this._obSelectBtn.skinsType="ann_obidaoju";
			}
			ms.visible=FlatInfo.isOpenSeeQb;
			this.getChildByName("button_addOB").visible=FlatInfo.isOpenSeeQb;
		}
		
		private function controlHzVisable():void
		{
			if(FlatInfo.isOpenHz)
			{
				if(FlatInfo.qqVipId==0)
				{
					this._kthzBtn.visible=true;
					this._ktnhzBtn.visible=false;
					this._xfhzBtn.visible=false;
					this._xfnhzBtn.visible=false;
				}else if(FlatInfo.qqVipId>0 && FlatInfo.qqVipId<101)
				{
					this._kthzBtn.visible=false;
					this._ktnhzBtn.visible=false;
					this._xfhzBtn.visible=true;
					this._xfnhzBtn.visible=false;
				}else if(FlatInfo.qqVipId>100){
					this._kthzBtn.visible=false;
					this._ktnhzBtn.visible=false;
					this._xfhzBtn.visible=false;
					this._xfnhzBtn.visible=true;
				}
			}else{
					this._kthzBtn.visible=
					this._ktnhzBtn.visible=
					this._xfhzBtn.visible=
					this._xfnhzBtn.visible=false;
			}
			
		}
		
		private function turnLeftHandler(e:ComponentEvent):void
		{
			this.nowPage-=1;
		}
		
		private function turnRightHandler(e:ComponentEvent):void
		{
			this.nowPage+=1;
		}
		
		/**
		 * 选择金币 
		 * @param e
		 * 
		 */	
		public function selectJB(e:ComponentEvent):void
		{
			this._jbSelectBtn["lockState"]=true;
			this._obSelectBtn["lockState"]=false;
			selectFlag="jb";
			setDetailData();
		}
		
		/**
		 * 选择O币 
		 * @param e
		 */	
		public function selectOB(e:ComponentEvent):void
		{
			this._jbSelectBtn["lockState"]=false;
			this._obSelectBtn["lockState"]=true;
			selectFlag="ob";
			setDetailData();
		}
		
		/**
		 * 物品 --类型为：101
		 * @param e
		 */		
		private function showCommonItemsHandler(e:ComponentEvent):void
		{
			this._button_common["lockState"]=true;
			this._button_weapon["lockState"]=false;
			this.selectTypeFlag="item";
			setDetailData();
		}
		/**
		 * 武器 --类型为：110,111,112,113,114
		 * @param e
		 */	
		private function showWeaponItemsHandler(e:ComponentEvent):void
		{
			this._button_common["lockState"]=false;
			this._button_weapon["lockState"]=true;
			this.selectTypeFlag="weapon";
			setDetailData();
		}
		
		private function updatePage():void
		{
			(this.getChildByName("pageTxt") as TextField).text=this.nowPage+"/"+this.maxPage;
			this.setLeftAndRightVisible();
		}
		
		public function getAllStoreData(obj:Object):void
		{
			this.jbArr=[];
			this.obArr=[];
			for each(var item:IGD_ShopItem in obj)
			{
				if(item.moneyType==0)
				{
					this.obArr.push(item);
				}else{
					this.jbArr.push(item);
				}
			}
			setDetailData();
		}
		
		private function setDetailData():void
		{
			var tmpMoneyArr:Array=this.selectFlag=="ob"?this.obArr:this.jbArr;
			switch(this.selectTypeFlag)
			{
				case "weapon":
					currentData=this.filterData(tmpMoneyArr,this.package_weapon_type);
					break;
				case "item":
					currentData=this.filterData(tmpMoneyArr,this.package_item_type);
					break;
			}
			setItemsAry();
		}
		public function setItemsAry():void
		{
			if(!currentData.length)
			{
				this.maxPage=1;
			}
			else
			{
				this.maxPage=int((this.currentData.length-1)/8)+1;
			}
			if(this.nowPage>this.maxPage)
			{
				this.nowPage=this.maxPage;
			}
			else
			{
				this.reshow();
			}
		}
		public function reshow():void
		{
			for(var i:int=0;i<8;i++)
			{
				var shopFrame:ShopItemFrame=this.getShopItemFrameByIndex(i);
				if(FlatInfo.isOPenQQVip && selectFlag=="ob" && FlatInfo.isOpenHz)
				{
					shopFrame._zhekouIcon.visible=true;
				}else{
					shopFrame._zhekouIcon.visible=false;
				}
				shopFrame._shopMb.visible=true;
				var item:IGD_ShopItem=currentData[i+(this.nowPage-1)*8] as IGD_ShopItem;
				if(null!=item)
				{
					shopFrame.visible=true;
					shopFrame.setData(item);
				}else{
					shopFrame.visible=false;
				}
			}
		}
		
		public function get shopItemFrame1():DisplayObject
		{
			return this.getShopItemFrameByIndex(0);
		}
		
		private  function getShopItemFrameByIndex(index:int):ShopItemFrame
		{
			if(index<0 || index>7)throw new Error("索引出错");
			return (this.getChildByName("shopItem"+(index+1))) as ShopItemFrame;
		}
		
		/**
		 * 过滤数据 
		 * @param dataArr  金币或者OB数组
		 * @param itemType 物品类型数组
		 * @return 
		 * 
		 */		
		private function filterData(dataArr:Array,itemType:Array):Array
		{
			var tmpArr:Array=new Array();
			var len:int=dataArr.length;
			for(var i:int=0;i<len;i++)
			{
				var sitem:IGD_ShopItem=dataArr[i] as IGD_ShopItem;
				for(var j:int=0;j<itemType.length;j++)
				{
					if(sitem.secondType==Number(itemType[j]))
					{
						if(FlatInfo.newGuiderShop && sitem.shopItemId==11100002)
						{
							tmpArr.unshift(sitem);
						}else{
							tmpArr.push(sitem);
						}
						break;
					}
				}
			}
			return tmpArr;
		}
	
		private function removeListener():void
		{
			
			(this.getChildByName("button_close") as MyButton).removeEventListener(ComponentEvent.BUTTON_DOWN,closeHandler);
		}
		
		public function showMyJB(value:int):void
		{
			(this.getChildByName("JbTxt") as TextField).text=String(value);
		}
		
		public function showMyOB(value:int):void
		{
			(this.getChildByName("ObTxt") as TextField).text=String(value);
		}
		
		private function closeHandler(e:ComponentEvent):void
		{
			this.dispatchEvent(new PanelEvent(PanelEvent.CLOSE,true));
		}
		
		private function addJBHandler(e:ComponentEvent):void
		{
			if(FlatInfo.isOpenExchangMoney)
			{
				this.dispatchEvent(new PanelEvent("showExChangePanel",true));
			}
			
		}
		
		/*getter & setter*/
		public function get nowPage():int
		{
			return _nowPage;
		}
		
		public function set nowPage(value:int):void
		{
			if(value==this.nowPage)return;
			if(value<1 || value>this.maxPage)return;
			_nowPage = value;
			this.updatePage();
			this.reshow();
		}
		
		public function get maxPage():int
		{
			return _maxPage;
		}
		
		public function set maxPage(value:int):void
		{
			if(value<0) return;
			_maxPage = value;
			this.updatePage();
		}
		
		//************************************************************************************ 设置左、右翻页按扭是否可见  **********
		/**
		 *设置左、右翻页按扭是否可见 
		 */
		private function setLeftAndRightVisible():void
		{
			var filter:ColorMatrixFilter = new ColorMatrixFilter([0.3,0.6,0,0,0,0.3,0.6,0,0,0,0.3,0.6,0,0,0,0,0,0,1,0]) ;
			
			var noShowLeft:Boolean		=		this._nowPage==1;
			var noShowRight:Boolean		=		this._nowPage==this._maxPage;
			
			if(!noShowLeft){
				//				(this.getChildByName("buttonLeft") as MyButton).visible				=		true;
				(this.getChildByName("leftBtn") as MyButton).mouseChildren			    =		true;
				(this.getChildByName("leftBtn") as MyButton).mouseEnabled			    =		true;
				(this.getChildByName("leftBtn") as MyButton).filters					=		null;
			}
			else
			{
				//				(this.getChildByName("leftBtn") as MyButton)						=		false;
				(this.getChildByName("leftBtn") as MyButton).mouseChildren				=		false;
				(this.getChildByName("leftBtn") as MyButton).mouseEnabled				=		false;
				(this.getChildByName("leftBtn") as MyButton).filters					=		[filter];
			}
			
			if(!noShowRight){
				//				(this.getChildByName("buttonRight") as MyButton).visible				=		true;
				(this.getChildByName("rightBtn") as MyButton).mouseChildren		=		true;
				(this.getChildByName("rightBtn") as MyButton).mouseEnabled			=		true;
				(this.getChildByName("rightBtn") as MyButton).filters				=		null;
			}
			else
			{
				//				(this.getChildByName("rightBtn") as MyButton).visible				=		false;
				(this.getChildByName("rightBtn") as MyButton).mouseChildren		=		false;
				(this.getChildByName("rightBtn") as MyButton).mouseEnabled			=		false;
				(this.getChildByName("rightBtn") as MyButton).filters				=		[filter];
			}
		}
	}
}